package com.mars.logger.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.logger.annotation.Log;
import com.mars.logger.dto.LoggerErrorDto;
import com.mars.logger.dto.LoggerLoginDto;
import com.mars.logger.dto.LoggerOperateDto;
import com.mars.logger.rpc.api.LoggerErrorService;
import com.mars.logger.rpc.api.LoggerLoginService;
import com.mars.logger.rpc.api.LoggerOperateService;

@RestController
@RequestMapping(value = "/api/logger")
public class LoggerController extends BaseController {

	@Autowired
	private LoggerLoginService loggerLoginService;
	
	@Autowired
	private LoggerOperateService loggerOperateService;
	
	@Autowired
	private LoggerErrorService loggerErrorService;
	
	/**
	 * 登录日志
	 * @return
	 */
	@Log(active=false)
	@PostMapping(value = "/selectLoggerByLogin")
	public Object selectLoggerByLogin(@RequestBody LoggerLoginDto dto) {
		return this.loggerLoginService.selectPage(new Paginator(dto.getPage(), dto.getRows(), dto.getOrderByLogin()), dto) ;
	}
	
	@Log(active=false)
	@GetMapping(value = "/deleteByLogin/{id}")
	public Object deleteByLogin(@PathVariable Integer id) {
		try {
			this.loggerLoginService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
	
	@Log(active=false)
	@GetMapping(value = "/getByLoginDetail/{id}")
	public Object getByLogin(@PathVariable Integer id) {
		return this.loggerLoginService.selectById(id);
	}
	
	
	/**
	 * 操作日志
	 * @param dto
	 * @return
	 */
	@Log(active=false)
	@PostMapping(value = "/selectLoggerByOperate")
	public Object selectLoggerByOperate(@RequestBody LoggerOperateDto dto) {
		return this.loggerOperateService.selectPage(new Paginator(dto.getPage(), dto.getRows(), dto.getOrderByOperate()), dto) ;
	}
	
	@Log(active=false)
	@GetMapping(value = "/deleteByOperate/{id}")
	public Object deleteByOperate(@PathVariable Integer id) {
		try {
			this.loggerOperateService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
	
	@Log(active=false)
	@GetMapping(value = "/getByOperateDetail/{id}")
	public Object getByOperateDetail(@PathVariable Integer id) {
		return this.loggerOperateService.selectById(id) ;
	}
	
	
	
	/**
	 * 错误日志
	 * @return
	 */
	@Log(active=false)
	@PostMapping(value = "/selectLoggerByError")
	public Object selectLoggerByError(@RequestBody LoggerErrorDto dto) {
		return this.loggerErrorService.selectPage(new Paginator(dto.getPage(), dto.getRows(), dto.getOrderByEexception()), dto) ;
	}
	
	@Log(active=false)
	@GetMapping(value = "/deleteByError/{id}")
	public Object deleteByError(@PathVariable Integer id) {
		try {
			this.loggerErrorService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
	
	@Log(active=false)
	@GetMapping(value = "/getByErrorDetail/{id}")
	public Object getByError(@PathVariable Integer id) {
		return this.loggerErrorService.selectById(id) ;
	}
	
}
